var crypto = require('crypto');
var  client = require('../database');
mysql = client.getDbCon();

exports.index = function(req, res){
  res.render('index', { title: '登录页面'});
};

exports.logout = function(req,res){
    req.session.user ="";
    res.redirect('/');
};

exports.toRegister = function(req,res){
    res.render('register',{title: '注册页面'});
};

exports.toMainPage = function(req,res){
    //线下值isonline值为0
    var selectbellowSQL = "select * from tb_serverlist where isonline= '" + 0 + "'";

    mysql.query(selectbellowSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
             bellowdata = JSON.stringify(rows);
            var user = req.session.user;
            return res.render('server_manager',{bellowdata:bellowdata,user:user});
        }
    });

};

//获取线上服务器列表数据
exports.getServerList = function(req,res){
    var selectNameSQL = "select * from tb_serverlist where isonline= '" + 1 + "'";
    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            res.writeHead(200,{"Content-Type":"text/plain"});
            res.end(data);
        }
    });
};

//增加一条服务器列表数据
exports.createServer = function(req,res){
    var servername = req.body.servername;
    var ip = req.body.ip;
    var config = req.body.config;
    var type = req.body.type;
    var status = req.body.status;
    var remark = req.body.remark;
    var isonline = req.body.isonline;

    var insertSQL = 'insert into tb_serverlist values(null,' +'"'+ servername +'"' +',' + '"'+ip+'"' +',' + '"'+config+'"' +',' +'"'+type+'"'+',' + '"'+ status +'"'+','+ '"'+ remark +'"' +','+ '"'+ isonline +'"' +')';

    mysql.query(insertSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};

exports.editServer = function(req,res){
    var servername = req.body.servername;
    var ip = req.body.ip;
    var config = req.body.config;
    var type = req.body.type;
    var status = req.body.status;
    var remark = req.body.remark;
    var isonline = req.body.isonline;
    var id = req.body.id;

    var updateSQL = 'update tb_serverlist set server_name=' +'"'+ servername +'"' +',' +'ip_address='+ '"'+ip+'"' +',' +'configuration='+ '"'+config+'"' +',' +'server_type='+'"'+type+'"'+',' +'status='+'"'+ status +'"'+','+'remark=' +'"'+ remark +'"' +','+ 'isonline='+'"'+ isonline +'"' +'where id='+'"'+ id +'"';

    mysql.query(updateSQL,function(err,rows){
        if (err) console.log(err);
        console.log("UPDATE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};

exports.deleteServer = function(req,res){
    var id = req.query['id'];
    var deleteSQL = 'delete from tb_serverlist  where id=' +'"'+ id +'"' ;
    mysql.query(deleteSQL,function(err,rows){
        if (err) console.log(err);
        console.log("DELETE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};